<?php

namespace App\Admin\Controllers;
use Illuminate\Support\Facades\DB;
use App\crm_hetong;
use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Chart\Bar;
use Encore\Admin\Widgets\Chart\Doughnut;
use Encore\Admin\Widgets\Chart\Line;
use Encore\Admin\Widgets\Chart\Pie;
use Encore\Admin\Widgets\Chart\PolarArea;
use Encore\Admin\Widgets\Chart\Radar;
use Encore\Admin\Widgets\Collapse;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;

class HomeController extends Controller
{
    public function index()
    {
        // dd(date("Y-m-d", strtotime("2009-10-21")-3600*24));
        return Admin::content(function (Content $content) {

            $content->header('统计分析');
            $content->description('分析...');
            $content->row(function (Row $row) {
                $row->column(4, function (Column $column) {
                    // 工单饼状图
                    $shangtan=DB::table("crm_order")->where("order_statue",1)->count();
                    $jinxing=DB::table("crm_order")->where("order_statue",2)->count();
                    $chenggong=DB::table("crm_order")->where("order_statue",4)->count();
                    $shibai=DB::table("crm_order")->where("order_statue",3)->count();
                    $pie = new Pie([
                        ['失败', $shibai],['商谈', $shangtan], ['进行', $jinxing],  ['成功', $chenggong],
                        
                    ]);
                    $column->append((new Box('工单统计', $pie))->style('primary')->solid());
                    // 销售统计
                    $yuangong=DB::table("admin_users")->get();
                    foreach($yuangong as $k=>$value){
                        $deal[$k]['name']=$value->username;
                        $deal[$k]['lirun']=0;
                        $deal[$k]['renshu']=0;
                        $li=DB::table("crm_deal")->where("deal_jiesuanstatue",3)->where("deal_userid",$value->id)->where("deal_jiesuandate",date("y-m"))->sum('deal_lirun');
                        $ren=DB::table("crm_deal")->where("deal_jiesuanstatue",3)->where("deal_userid",$value->id)->where("deal_jiesuandate",date("y-m"))->sum('deal_num');
                        if($li){
                           $deal[$k]['lirun']=$li;
                        }
                        if($ren){
                           $deal[$k]['renshu']=$ren;
                        }
                    }
                    $len=count($deal);
                    for($i=1;$i<$len;$i++)
                    { //该层循环用来控制每轮 冒出一个数 需要比较的次数
                        for($k=0;$k<$len-$i;$k++)
                        {
                           if($deal[$k]['lirun']<$deal[$k+1]['lirun'])
                            {
                                $tmp=$deal[$k+1];
                                $deal[$k+1]=$deal[$k];
                                $deal[$k]=$tmp;
                            }
                        }
                    }
                    if($len<5){
                        for($i=0;$i<$len;$i++){
                            $name[$i]=$deal[$i]['name'];
                            $lirun[$i]=$deal[$i]['lirun'];
                            $renshu[$i]=$deal[$i]['renshu'];
                        }
                    }else{
                        for($i=0;$i<5;$i++){
                            $name[$i]=$deal[$i]['name'];
                            $lirun[$i]=$deal[$i]['lirun'];
                            $renshu[$i]=$deal[$i]['renshu'];
                        }
                    }
                    $bar = new Bar(
                        $name,
                        [
                            ['人数', $renshu],
                            ['利润', $lirun],
                        ]
                    );
                    $column->append((new Box('销售统计', $bar))->style('primary')->solid());
                });
                $row->column(4, function (Column $column) {
                    // 成交单饼状图
                    $wei=DB::table("crm_deal")->where("deal_jiesuanstatue",1)->count();
                    $jinxing=DB::table("crm_deal")->where("deal_jiesuanstatue",2)->count();
                    $yi=DB::table("crm_deal")->where("deal_jiesuanstatue",3)->count();
                    $chengjiao = new Pie([
                        ['未结算', $wei],['结算中', $jinxing], ['已结算', $yi],  
                        
                    ]);
                    $column->append((new Box('成交单统计', $chengjiao))->style('primary')->solid());
                    // 客户统计
                    $kehu=DB::table('crm_customer')->get();
                    foreach($kehu as $k=>$vo){
                        $kehutongji[$k]['name']=$vo->customer_name;
                        $kehutongji[$k]['lirun']=0;
                        $kehus=DB::table("crm_deal")->where("deal_customer_id",$vo->id)->where("deal_jiesuanstatue",3)->where("deal_jiesuandate",date("y-m"))->get();
                        if(!$kehus->isEmpty()){
                            foreach($kehus as $k=>$vo){
                                $a[$k]=$vo->deal_lirun;
                            }
                            $kehutongji[$k]['lirun']=array_sum($a);
                        }
                    }
                    $lenkehu=count($kehutongji);
                    for($i=1;$i<$lenkehu;$i++)
                    { //该层循环用来控制每轮 冒出一个数 需要比较的次数
                        for($k=0;$k<$lenkehu-$i;$k++)
                        {
                           if($kehutongji[$k]['lirun']<$kehutongji[$k+1]['lirun'])
                            {
                                $tmp=$kehutongji[$k+1];
                                $kehutongji[$k+1]=$kehutongji[$k];
                                $kehutongji[$k]=$tmp;
                            }
                        }
                    }
                    if($lenkehu<5){
                        for($i=0;$i<$lenkehu;$i++){
                            $kehuname[$i]=$kehutongji[$i]['name'];
                            $kehulirun[$i]=$kehutongji[$i]['lirun'];
                        }
                    }else{
                        for($i=0;$i<5;$i++){
                            $kehuname[$i]=$kehutongji[$i]['name'];
                            $kehulirun[$i]=$kehutongji[$i]['lirun'];
                        }
                    }
                    
                    $barkehu = new Bar(
                        $kehuname,
                        [
                            ['利润', $kehulirun],
                        ]
                    );
                    $column->append((new Box('客户统计', $barkehu))->style('primary')->solid());
                    
                    
                });
                $row->column(4, function (Column $column) {
                    // 合同饼状图
                    $ruku=crm_hetong::where("statue",1)->count();
                    $liangyong=crm_hetong::where("statue",2)->count();
                    $daihexiao=crm_hetong::where("statue",3)->count();
                    $hexiao=crm_hetong::where("statue",4)->count();
                    $hetong = new Pie([
                        ['入库', $ruku],['领用', $liangyong], ['核销', $hexiao], ['待核销', $daihexiao],  
                    ]);
                    $column->append((new Box('合同统计', $hetong))->style('primary')->solid());
                    
                    // 供应商统计
                    $maoli=DB::table('crm_supplier')->get();
                    foreach($maoli as $k=>$vo){
                        $maoli[$k]->renshu=0;
                        $maoli[$k]->dakuan=0;
                        $deals=DB::table("crm_deal_crm_supplier")->where("crm_supplier_id",$vo->id)->pluck("crm_deal_id");
                        $renshu=DB::table("crm_deal")->whereIn("id",$deals)->where("deal_jiesuanstatue",3)->where("deal_jiesuandate",date("y-m"))->get();
                        if(!$deals->isEmpty() && !$renshu->isEmpty()){
                            foreach($renshu as $k=>$vo){
                                $a[$k]=$vo->deal_num;
                                $b[$k]=$vo->deal_sdakuan;
                            }
                            $maoli[$k]->renshu=array_sum($a);
                            $maoli[$k]->dakuan=array_sum($b);
                        }
                    }
                    $lens=count($maoli);
                    for($i=1;$i<$lens;$i++)
                    { //该层循环用来控制每轮 冒出一个数 需要比较的次数
                        for($k=0;$k<$lens-$i;$k++)
                        {
                           if($maoli[$k]->dakuan<$maoli[$k+1]->dakuan)
                            {
                                $tmp=$maoli[$k+1];
                                $maoli[$k+1]=$maoli[$k];
                                $maoli[$k]=$tmp;
                            }
                        }
                    }
                    if($lens<5){
                       for($i=0;$i<$lens;$i++){
                            $name2[$i]=$maoli[$i]->supplier_logoname;
                            $dakuan2[$i]=$maoli[$i]->dakuan;
                            $renshu2[$i]=$maoli[$i]->renshu;
                        } 
                    }else{
                       for($i=0;$i<5;$i++){
                            $name2[$i]=$maoli[$i]->supplier_logoname;
                            $dakuan2[$i]=$maoli[$i]->dakuan;
                            $renshu2[$i]=$maoli[$i]->renshu;
                        }  
                    }
                    $bars = new Bar(
                        $name2,
                        [
                            ['人数', $renshu2],
                            ['打款', $dakuan2],
                        ]
                    );
                    $column->append((new Box('供应商统计', $bars))->style('primary')->solid());
                });
            });

        }); 
    }
    public function geren()
    {
        return redirect(admin_url('auth/setting')); 
    }
}
